<?php
namespace controllers;
use controllers\api;
use Model\supplier;
use Model\suppliers;
use SMB\Buyer\buyer_batch;
use SMB\Buyer\buyer_order;
use Model\supplier_log;
use SMB\Supplier\supplier_way;
use Model\orders;
use SMB\Goods\supplier_goods_relation;
use SMB\Stocks\purchase_order_new;
use SMB\Stocks\batch;
use Model\notice;
use SMB\Utils\DingTalkRobot;
class order extends api{
    //供应商订单相关数量
    public function number()
    {
        //两天前的时间戳
        $time = date('Y-m-d',time());
        $zero_time = strtotime("$time 00:00:00");
        $start_time = $zero_time-86400*2;
        //获取供货商信息
        $supplier_model = new suppliers();
        $supplier_info = $supplier_model->get_sup_info();

        //获取订单数量
        $new_order_ob = $this->get_order_num($supplier_info['_id'],1,$start_time);
        $a = 0;
        $purchase_order_model = new purchase_order_new();
        foreach ($new_order_ob as $v){
            $purchase_order_new_list = $purchase_order_model->find(array('batch_id'=>$v['_id'],'status'=>5));
            $purchase_order_new_list = iterator_to_array($purchase_order_new_list);
            if(!isset($v['sign']) && !empty($purchase_order_new_list)){
                $a++;
            }
            unset($purchase_order_new_list);
        }

        //重组数据
        $data = array();
        //新订单数量
        $data[0] = $a;
        //待发货订单数量

        $data[1]= count($this->get_order_nums($supplier_info['_id'],1,1,$start_time));
        //待收货订单数量
        $data[2]  = count($this->get_order_nums($supplier_info['_id'],1,2,$start_time));
        //已完成数量
        $num = 0;
        $complete_list = $this->get_order_num($supplier_info['_id'],5,$start_time);
        foreach ($complete_list as $v){
            $purchase_order_new_list = $purchase_order_model->find(array('batch_id'=>$v['_id'],'status'=>15));
            $purchase_order_new_list = iterator_to_array($purchase_order_new_list);
            if(!empty($purchase_order_new_list)){
                $num++;
            }
            unset($purchase_order_new_list);
        }
        $data[3]  = $num;
        //已取消订单数量
        $nums = 0;
        $cancel_list  = $this->get_order_num($supplier_info['_id'],1,$start_time);
        foreach ($cancel_list as $v){
            $purchase_order_new_list = $purchase_order_model->find(array('batch_id'=>$v['_id'],'status'=>10));
            $purchase_order_new_list = iterator_to_array($purchase_order_new_list);
            if(!empty($purchase_order_new_list)){
                $num++;
            }
            unset($purchase_order_new_list);
        }
        $data[4]  = $nums;
        return $data;
    }


    //新订单详情
    public function new_order_info(){
        //两天前的时间戳
        $time = date('Y-m-d',time());
        $zero_time = strtotime("$time 00:00:00");
        $start_time = $zero_time-86400*2;
        //获取供货商信息
        $supplier_db = new suppliers();
        $supplier_info = $supplier_db->get_sup_info();

        //获取当前供应商所有新订单
        $db = new batch();
        //$buyer_batch_arr = array();
        $buyer_batch_list = $db->find(array('create_time'=>array('$gt'=>$start_time),'status'=>1,'supplier_id'=>(string)$supplier_info['_id']));



        //        foreach ($buyer_batch as $buyer){
//            if($buyer['supplier_id'] == (string)$supplier_info['_id']){
//                array_push($buyer_batch_arr,$buyer);
//            }
//        }

        //订单数量
        $order_nums = $this->number();
        //结算方式
        $supplier_db = new supplier_way();
        $payment_way_arr = $supplier_db->get_way('payment_way');
        $data = array();
        $buyer_order_db = new purchase_order_new();
        $new_oder_details = array();
        foreach ($buyer_batch_list as $v){
            if(isset($v['sign'])){
                continue;
            }
            //查询所有新订单订购详情
            $new_order_list = $buyer_order_db->find(array('batch_id'=>$v['_id'],'status'=>5,'create_time'=>array('$gt'=>$start_time)));
            if(empty(iterator_to_array($new_order_list))){
                continue;
            }
            $accept_oder_details_arr['supplier'] = $supplier_info['supplier_name'];//供应商
            $accept_oder_details_arr['id'] = $v['code'];//订单编号
            $accept_oder_details_arr['_id'] = $v['_id'];//订单ID
            $accept_oder_details_arr['mode'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式;
            $accept_oder_details_arr['more'] = false;
            $accept_oder_details_arr['items'] = array();
            $buyer_name = '';
            $new_oder_detail = array();
            foreach ($new_order_list as $value){
                $new_oder_detail['id'] = $value['id'];//商品编号
                $new_oder_detail['name'] = $value['product_info']['product_title'];//商品名称
                $new_oder_detail['barcode'] = $value['product_info']['barcode'];//商品条码
                //获取采购规格参数
                $specification_procurement = $value['product_info']['specification_procurement'];
                $specification_arr = $value['product_info']['specification_arr'][$specification_procurement];
                //规格
                $specification_str = !empty($specification_arr['specification']) ? $specification_arr['specification'] : ($specification_procurement == 1 ? '整份' : '小份');
                //一件的规格
                $one_specification_str = $value['product_info']['specification_arr'][1]['specification'];
                //规格数量
                $units = $specification_arr['units'];
                if($specification_procurement==1){
                    $str_spe = $units.$specification_str;
                }else{
                    $str_spe = $specification_str.'('.$units.$one_specification_str.')';
                }
                $new_oder_detail['num'] = $value['product_info']['count'];
                $new_oder_detail['category'] = $new_oder_detail['num'].$str_spe;
                $new_oder_detail['unitprice'] = $value['product_info']['total_price']/(100*$value['product_info']['count']);//采购单价
                $time  = date('Y-m-d H:i',$value['create_time']);//
                $buyer_name =  $value['buyer']['nickname'];
                array_push($accept_oder_details_arr['items'],$new_oder_detail);
            }
            $new_oder_detail_arr['time'] = $time;//创建时间
            $accept_oder_details_arr['purchaser'] = $buyer_name;//采购者
            array_push($new_oder_details,$accept_oder_details_arr);
        }
        //var_dump($new_oder_details);exit;
        $this->success($new_oder_details,$order_nums);
    }
    //待发货订单详情
    public function delivery_info(){
        //两天前的时间戳
        $time = date('Y-m-d',time());
        $zero_time = strtotime("$time 00:00:00");
        $start_time = $zero_time-86400*2;
        //获取供货商信息
        $supplier_db = new suppliers();
        $supplier_info = $supplier_db->get_sup_info();


        //根据日志获取订单的接受时间与发货时间
        $log_model = new supplier_log();
        $log_arr = $log_model->getSupplier_log();

        //获取当前供应商所有待发货订单
        $db = new batch();
//        $buyer_batch_arr = array();
//        $buyer_batch = $db->find(array('create_time'=>array('$gt'=>$start_time),'sign'=>1,'status'=>5));
//        foreach ($buyer_batch as $buyer){
//            if($buyer['supplier_id'] == (string)$supplier_info['_id']){
//                array_push($buyer_batch_arr,$buyer);
//            }
//        }
        $buyer_batch_list = $db->find(array('create_time'=>array('$gt'=>$start_time),'status'=>1,'sign'=>1,'supplier_id'=>(string)$supplier_info['_id']));
        //订单数量
        $order_nums = $this->number();
        //结算方式
        $supplier_db = new supplier_way();
        $payment_way_arr = $supplier_db->get_way('payment_way');


        $data = array();


        $buyer_order_db = new purchase_order_new();
        $accept_oder_details_ls = array();
        foreach ($buyer_batch_list as $v){
            $accept_order_ob = $buyer_order_db->find(array('batch_id'=>$v['_id'],'status'=>5,'create_time'=>array('$gt'=>$start_time)));
            $accept_oder_details_arr['supplier'] = $supplier_info['supplier_name'];
            $accept_oder_details_arr['id'] = $v['code'];
            $accept_oder_details_arr['_id'] = $v['_id'];
            $accept_oder_details_arr['mode'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式;
            $accept_oder_details_arr['more'] = false;
            $accept_oder_details_arr['items'] = array();
            $buyer_name = '';
            $accept_time = '';
            foreach ($accept_order_ob as $value){
                //var_dump($value);
                $accept_oder_arr['id'] = $value['id'];
                $accept_oder_arr['name'] = $value['product_info']['product_title'];
                $accept_oder_arr['barcode'] = $value['product_info']['barcode'];
                //获取采购规格参数
                $specification_procurement = $value['product_info']['specification_procurement'];
                $specification_arr = $value['product_info']['specification_arr'][$specification_procurement];
                //规格
                $specification_str = !empty($specification_arr['specification']) ? $specification_arr['specification'] : ($specification_procurement == 1 ? '整份' : '小份');
                //一件的规格
                $one_specification_str = $value['product_info']['specification_arr'][1]['specification'];
                //规格数量
                $units = $specification_arr['units'];
                if($specification_procurement==1){
                    $str_spe = $units.$specification_str;
                }else{
                    $str_spe = $specification_str.'('.$units.$one_specification_str.')';
                }
                $accept_oder_arr['num'] = $value['product_info']['count'];
                $accept_oder_arr['category'] = $accept_oder_arr['num'].$str_spe;
                $accept_oder_arr['unitprice'] = $value['product_info']['total_price']/($value['product_info']['count']*100);
                $time  = date('Y-m-d H:i',$value['create_time']);
                $accept_time = isset($log_arr[$v['supplier_id'.''].$value['batch_id'.''].'1'])?date('Y-m-d H:i:s',$log_arr[$v['supplier_id'.''].$value['batch_id'.''].'1']):'';
                $buyer_name = $value['buyer']['nickname'];
                $accept_oder_details_arr['items'][] = $accept_oder_arr;
            }
            $accept_oder_details_arr['time'] = $time;
            $accept_oder_details_arr['accept_time'] = $accept_time;

            $accept_oder_details_arr['purchaser'] = $buyer_name;

            array_push($accept_oder_details_ls,$accept_oder_details_arr);
        }
//        var_dump($accept_oder_details_arr);exit;
        $this->success($accept_oder_details_ls,$order_nums);
    }
    //待收货订单详情
    public function receipt_info(){
        //两天前的时间戳
        $time = date('Y-m-d',time());
        $zero_time = strtotime("$time 00:00:00");
        $start_time = $zero_time-86400*2;
        //获取供货商信息
        $supplier_db = new suppliers();
        $supplier_info = $supplier_db->get_sup_info();
        //获取当前供应商所有待收货的订单
        $db = new batch();
        $buyer_batch_list = $db->find(array('create_time'=>array('$gt'=>$start_time),'status'=>1,'sign'=>2,'supplier_id'=>(string)$supplier_info['_id']));
        //订单数量
        $order_nums = $this->number();


        //根据日志获取订单的接受时间与发货时间
        $log_model = new supplier_log();
        $log_arr = $log_model->getSupplier_log();
//        var_dump($log_arr);


        //结算方式
        $supplier_db = new supplier_way();
        $payment_way_arr = $supplier_db->get_way('payment_way');
        $data = array();
        $buyer_order_db = new purchase_order_new();
        $accept_oder_details_ls = array();
        foreach ($buyer_batch_list as $v){
            $accept_order_ob = $buyer_order_db->find(array('batch_id'=>$v['_id'],'status'=>5,'create_time'=>array('$gt'=>$start_time)));
            $accept_oder_details_arr['supplier'] = $supplier_info['supplier_name'];
            $accept_oder_details_arr['id'] = $v['code'];
            $accept_oder_details_arr['_id'] = $v['_id'];
            $accept_oder_details_arr['mode'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式;
            $accept_oder_details_arr['more'] = false;
            $accept_oder_details_arr['items'] = array();
            $accept_oder_details_arr['info']['name'] = $v['driver_info']['name'];
            $accept_oder_details_arr['info']['license'] = $v['driver_info']['license'];
            $accept_oder_details_arr['info']['tel'] = $v['driver_info']['tel'];
            $buyer_name = '';
            $accept_time = '';
            $send_time = '';
            foreach ($accept_order_ob as $value){
                //var_dump($value);
                $accept_oder_arr['id'] = $value['id'];
                $accept_oder_arr['name'] = $value['product_info']['product_title'];
                $accept_oder_arr['barcode'] = $value['product_info']['barcode'];
                //获取采购规格参数
                $specification_procurement = $value['product_info']['specification_procurement'];
                $specification_arr = $value['product_info']['specification_arr'][$specification_procurement];
                //规格
                $specification_str = !empty($specification_arr['specification']) ? $specification_arr['specification'] : ($specification_procurement == 1 ? '整份' : '小份');
                //一件的规格
                $one_specification_str = $value['product_info']['specification_arr'][1]['specification'];
                //规格数量
                $units = $specification_arr['units'];
                if($specification_procurement==1){
                    $str_spe = $units.$specification_str;
                }else{
                    $str_spe = $specification_str.'('.$units.$one_specification_str.')';
                }
                $accept_oder_arr['num'] = $value['product_info']['count'];
                $accept_oder_arr['category'] = $accept_oder_arr['num'].$str_spe;
                $accept_oder_arr['unitprice'] = $value['product_info']['total_price']/($value['product_info']['count']*100);
                $time  = date('Y-m-d H:i',$value['create_time']);
                $accept_time = isset($log_arr[$v['supplier_id'.''].$value['batch_id'.''].'1'])?date('Y-m-d H:i:s',$log_arr[$v['supplier_id'.''].$value['batch_id'.''].'1']):'';
                $send_time =isset($log_arr[$v['supplier_id'.''].$value['batch_id'.''].'2'])?date('Y-m-d H:i:s',$log_arr[$v['supplier_id'.''].$value['batch_id'.''].'2']):'';
                $buyer_name = $value['buyer']['nickname'];
                $accept_oder_details_arr['items'][] = $accept_oder_arr;
            }
            $accept_oder_details_arr['time'] = $time;
            $accept_oder_details_arr['accept_time'] = $accept_time;
            $accept_oder_details_arr['send_time'] = $send_time;
            $accept_oder_details_arr['purchaser'] = $buyer_name;
            array_push($accept_oder_details_ls,$accept_oder_details_arr);
        }
        $this->success($accept_oder_details_ls,$order_nums);
    }
    //已完成订单详情
    public function complete_info(){
        //两天前的时间戳
        $time = date('Y-m-d',time());
        $zero_time = strtotime("$time 00:00:00");
        $start_time = $zero_time-86400*2;
        //获取供货商信息
        $supplier_db = new suppliers();
        $supplier_info = $supplier_db->get_sup_info();

        //获取当前供应商所有完成的订单
        $db = new batch();
        $buyer_batch_list = $db->find(array('create_time'=>array('$gt'=>$start_time),'status'=>5,'supplier_id'=>(string)$supplier_info['_id']));
        $order_nums = $this->number();

        //根据日志获取订单的接受时间与发货时间
        $log_model = new supplier_log();
        $log_arr = $log_model->getSupplier_log();

        //根据消息获取订单完成时间
        $notice_model  = new notice();
        $notice_arr = $notice_model->getComplete_time();

        //结算方式
        $supplier_db = new supplier_way();
        $payment_way_arr = $supplier_db->get_way('payment_way');
        $data = array();
        $buyer_order_db = new purchase_order_new();
        $accept_oder_details_ls = array();
        foreach ($buyer_batch_list as $v){
            $accept_order_ob = $buyer_order_db->find(array('batch_id'=>$v['_id'],'status'=>15,'create_time'=>array('$gt'=>$start_time)));
            $accept_oder_details_arr['supplier'] = $supplier_info['supplier_name'];
            $accept_oder_details_arr['id'] = $v['id'];
            $accept_oder_details_arr['_id'] = $v['_id'];
            $accept_oder_details_arr['mode'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式;
            $accept_oder_details_arr['more'] = false;
            $accept_oder_details_arr['items'] = array();
            $buyer_name = '';
            $accept_oder_arr = array();
            $accept_oder_details_arr['info']['name'] = @$v['driver_info']['name'];
            $accept_oder_details_arr['info']['license'] = @$v['driver_info']['license'];
            $accept_oder_details_arr['info']['tel'] = @$v['driver_info']['tel'];
            $accept_time = '';
            $complete_time = '';
            $send_time = '';
            foreach ($accept_order_ob as $value){
                $accept_oder_arr['id'] = $value['id'];
                $accept_oder_arr['name'] = $value['product_info']['product_title'];
                $accept_oder_arr['barcode'] = $value['product_info']['barcode'];
                //获取采购规格参数
                $specification_procurement = $value['product_info']['specification_procurement'];
                $specification_arr = $value['product_info']['specification_arr'][$specification_procurement];
                //规格
                $specification_str = !empty($specification_arr['specification']) ? $specification_arr['specification'] : ($specification_procurement == 1 ? '整份' : '小份');
                //一件的规格
                $one_specification_str = $value['product_info']['specification_arr'][1]['specification'];
                //规格数量
                $units = $specification_arr['units'];
                if($specification_procurement==1){
                    $str_spe = $units.$specification_str;
                }else{
                    $str_spe = $specification_str.'('.$units.$one_specification_str.')';
                }
                $accept_oder_arr['num'] = $value['product_info']['count'];
                $accept_oder_arr['category'] = $accept_oder_arr['num'].$str_spe;
                $accept_oder_arr['unitprice'] = $value['product_info']['total_price']/($value['product_info']['count']*100);
                $time  = date('Y-m-d H:i',$value['create_time']);
                $accept_time = isset($log_arr[$v['supplier_id'.''].$value['batch_id'.''].'1'])?date('Y-m-d H:i:s',$log_arr[$v['supplier_id'.''].$value['batch_id'.''].'1']):'';
                $send_time =isset($log_arr[$v['supplier_id'.''].$value['batch_id'.''].'2'])?date('Y-m-d H:i:s',$log_arr[$v['supplier_id'.''].$value['batch_id'.''].'2']):'';
                $complete_time = isset($notice_arr[$v['supplier_id'.''].$value['batch_id'.'']])?date('Y-m-d H:i:s',$notice_arr[$v['supplier_id'.''].$value['batch_id'.'']]):'';
                $buyer_name = $value['buyer']['nickname'];
                $accept_oder_details_arr['items'][] = $accept_oder_arr;
            }
            $accept_oder_details_arr['time'] = $time;
            $accept_oder_details_arr['accept_time'] = $accept_time;
            $accept_oder_details_arr['complete_time'] = $complete_time;
            $accept_oder_details_arr['send_time'] = $send_time;
            $accept_oder_details_arr['purchaser'] = $buyer_name;

            array_push($accept_oder_details_ls,$accept_oder_details_arr);
        }
//        var_dump($accept_oder_details_arr);exit;
        $this->success($accept_oder_details_ls,$order_nums);
    }
    //取消订单详情
    public function cancel_info(){
        //两天前的时间戳
        $time = date('Y-m-d',time());
        $zero_time = strtotime("$time 00:00:00");
        $start_time = $zero_time-86400*2;
        //获取供货商信息
        $supplier_db = new suppliers();
        $supplier_info = $supplier_db->get_sup_info();

        //获取当前供应商所有取消的订单
        $db = new batch();
        $buyer_batch_arr = array();
//        $buyer_batch_list = array();
//        $buyer_batch = $db->find(array('create_time'=>array('$gt'=>$start_time),'status'=>5,''=>4));
//        foreach ($buyer_batch as $buyer){
//            if($buyer['supplier_id'] == (string)$supplier_info['_id']){
//                array_push($buyer_batch_arr,$buyer);
//            }
//        }
        $buyer_batch_list = $db->find(array('create_time'=>array('$gt'=>$start_time),1,'supplier_id'=>(string)$supplier_info['_id']));
        $order_nums = $this->number();

        //结算方式
        $supplier_db = new supplier_way();
        $payment_way_arr = $supplier_db->get_way('payment_way');
        $data = array();
        $buyer_order_db = new purchase_order_new();

        $accept_oder_details_ls = array();
        foreach ($buyer_batch_list as $v){
            $accept_order_ob = $buyer_order_db->find(array('batch_id'=>(string)$v['_id'],'status'=>10,'create_time'=>array('$gt'=>$start_time)));
            if(empty($accept_order_ob)){
                continue;
            }
            $accept_oder_details_arr['supplier'] = $supplier_info['supplier_name'];
            $accept_oder_details_arr['id'] = $v['code'];
            $accept_oder_details_arr['_id'] = $v['_id'];
            $accept_oder_details_arr['mode'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式;
            $accept_oder_details_arr['more'] = false;
            $accept_oder_details_arr['items'] = array();
            $buyer_name = '';
            foreach ($accept_order_ob as $value){
                $accept_oder_arr['id'] = $value['id'];
                $accept_oder_arr['name'] = $value['product_info']['product_title'];
                $accept_oder_arr['barcode'] = $value['product_info']['barcode'];
                $accept_oder_arr['num'] = $value['product_info']['count'];
                $accept_oder_arr['unitprice'] = $value['product_info']['total_price']/($value['product_info']['count']*100);
                $time  = date('Y-m-d H:i',$value['create_time']);
                $buyer_name = $value['buyer']['nickname'];
                $accept_oder_details_arr['items'][] = $accept_oder_arr;
            }
            $accept_oder_details_arr['time'] = $time;
            $accept_oder_details_arr['purchaser'] = $buyer_name;
            array_push($accept_oder_details_ls,$accept_oder_details_arr);
        }
//        var_dump($accept_oder_details_arr);exit;
        $this->success($accept_oder_details_ls,$order_nums);
    }

    //接受订单
    public  function accept_order(){
        if(isset($_REQUEST['purchases'])){
            if(!empty($_REQUEST['purchases'])){
                $res1 = 0;
                $batch_db = new batch();
                $supplier_db = new suppliers();
                $supplier_info = $supplier_db->get_sup_info();
                foreach ($_REQUEST['purchases'] as $v){
                    $_id = $v['_id']['$id'];
                    $batch_num = $v['id'];
                    $res = $batch_db->update(array('_id'=>new \MongoId($_id)),array('$set'=>array('sign'=>1,'accept_time'=>time())));
                    if($res['ok']){
                        $describe = '接受了订单，采购批次号为:'.$batch_num;
                        $supplier_log_db = new supplier_log();
                        $result = $supplier_log_db->add($supplier_info['supplier_name'],$supplier_info['_id'],$describe,$_id,'',1);
                        if($result){
                            $notice_model = new notice();
                            $res1 = $notice_model->add($describe,$supplier_info['supplier_name'],$supplier_info['_id'],$_id,'admin');
                            if($res1){
                                $data['name'] = $supplier_info['supplier_name'];
                                $data['time'] = time();
                                $data['describe'] = $describe;
                                DingTalkRobot::send_supplier($data);
                            }
                        }
                    }
                }
                if($res1){
                    $this->success('200');
                }else{
                    $this->error('错误');
                }
            }
        }
    }
    //发货
    public  function send_order(){
        if(isset($_REQUEST['id'])) {
            if (!empty($_REQUEST['id'])) {
                $res1 = 0;
                $batch_db = new batch();
                $supplier_db = new suppliers();
                $supplier_info = $supplier_db->get_sup_info();
                $date['name'] = $_REQUEST['name'];
                $date['license'] = $_REQUEST['license'];
                $date['tel'] = $_REQUEST['tel'];
                foreach ($_REQUEST['id'] as $v) {
                    $_id = $v['_id']['$id'];
                    $batch_num = $v['id'];
                    $res = $batch_db->update(array('_id' => new \MongoId($_id)), array('$set' => array('sign' =>2,'driver_info'=>$date,'send_time'=>time())));
                    if ($res['ok']) {
                        $describe = '已发货，采购批次号为:' . $batch_num;
                        $supplier_log_db = new supplier_log();
                        $result = $supplier_log_db->add($supplier_info['supplier_name'], $supplier_info['_id'], $describe, $_id,'',2);
                        if ($result) {
                            $notice_model = new notice();
                            $res1 = $notice_model->add($describe, $supplier_info['supplier_name'],$supplier_info['_id'] ,$_id, 'admin');
                            if($res1){
                                $data['name'] = $supplier_info['supplier_name'];
                                $data['time'] = time();
                                $data['describe'] = $describe;
                                DingTalkRobot::send_supplier($data);
                            }
                        }
                    }
                }
                if ($res1) {
                    $this->success('200');
                } else {
                    $this->error('错误');
                }
            }
        }
    }
    /*
     * 查询某个供货商下某个时间段的批次
     * $supplier_info 供货商信息 必须有_id
     * $start_time 时间
     */
    public function get_sup_batch($supplier_info,$start_time){
        $db = new buyer_batch();
        $buyer_batch_arr = array();
        $buyer_batch_ob = $db->find(array('create_time'=>array('$gt'=>$start_time)));
        foreach ($buyer_batch_ob as $buyer){
            if($buyer['supplier_id'] == (string)$supplier_info['_id']){
                array_push($buyer_batch_arr,(string)$buyer['_id']);
            }
        }
        return $buyer_batch_arr;
    }
    /*
     * $supplier_id 供应商id
     * $start_time 时间 大于这个时间小于当前时间
     */
    public function get_order_num($supplier_id,$status,$start_time){
        $db = new batch();
        $num = 0;
        $num = $db->find(array('supplier_id'=>(string)$supplier_id,'status'=>$status,'create_time'=>array('$gt'=>$start_time)));
        return iterator_to_array($num);
    }
    public function get_order_nums($supplier_id,$status,$sign,$start_time){
        $db = new batch();
        $num = 0;
        $num = $db->find(array('supplier_id'=>(string)$supplier_id,'status'=>$status,'sign'=>$sign,'create_time'=>array('$gt'=>$start_time)));
        return iterator_to_array($num);
    }

    //供应商第三方订单详情
    public function order(){
        $a = 2;
        if($a == 1) {
            $order_db = new orders();
            $order = $order_db->find(array('is_pre_order' => 1), array('receiver', 'order_code', 'order_create_time', 'pay_type', 'order_total_price', 'product'));
            $supplier_db = new suppliers();
            $res = $supplier_db->findOne(array('receiver.telNumber' => '15723095049'));
            $supplier_relation = new supplier_goods_relation();
            $rev = $supplier_relation->find(array('supplier_id' => $res['_id']), array('sku_id'));
            $data = array();
            $order_arr = array();
            $pay_type_arr = array('' => '所有支付方式', 0 => '货到付款', 1 => '微信支付', 2 => '支付宝支付', 3 => '银行卡支付');
            foreach ($order as $v) {
//            foreach ($rev as $vv){
//                if($v['product'][0]['sku_id'] == $vv['sku_id']){

                $order_arr[$v['product'][0]['sku_id']]['order_code'] = $v['order_code'];
                $order_arr[$v['product'][0]['sku_id']]['order_create_time'] = $v['order_create_time'];
                $order_arr[$v['product'][0]['sku_id']]['receiver_address'] = $v['receiver']['proviceFirstStageName'] . $v['receiver']['addressCitySecondStageName'] . $v['receiver']['addressCountiesThirdStageName'] . $v['receiver']['addressDetailInfo'];
                $order_arr[$v['product'][0]['sku_id']]['receiver_name'] = $v['receiver']['userName'];
                $order_arr[$v['product'][0]['sku_id']]['telNumber'] = $v['receiver']['telNumber'];
                $order_arr[$v['product'][0]['sku_id']]['pay_type'] = $pay_type_arr[$v['pay_type']];
                $order_arr[$v['product'][0]['sku_id']]['order_total_price'] = $v['order_total_price'];
//                }
//            }

            }
            $arr = array();
            foreach ($rev as $v) {
                //var_dump($v);
                if (!isset($order_arr[$v['sku_id']]['order_code'])) {
                    continue;
                }
                $arr['order_code'] = $order_arr[$v['sku_id']]['order_code'];
                $arr['order_create_time'] = $order_arr[$v['sku_id']]['order_create_time'];
                $arr['address'] = $order_arr[$v['sku_id']]['receiver_address'];
                $arr['receiver_name'] = $order_arr[$v['sku_id']]['receiver_name'];
                $arr['receiver_tel'] = $order_arr[$v['sku_id']]['telNumber'];
                $arr['pay_type'] = $order_arr[$v['sku_id']]['pay_type'];
                $arr['order_total_price'] = $order_arr[$v['sku_id']]['order_total_price'];
            }
            $this->success($arr);
        }

    }
    //对账单
    public function accounts(){
        $batch_model = new batch();
        //获取供货商信息
        $supplier_db = new suppliers();
        $supplier_way_model = new supplier_way();
        $supplier_info = $supplier_db->get_sup_info();
        //结算方式
        $payment_way_arr = $supplier_way_model->get_way('payment_way');

        //待确认账单
        $batch_list = $batch_model->find(array('status'=>5,'supplier_id'=>(string)$supplier_info['_id']))->sort(array('create_time'=>-1));
        $new_batch_arr = array();
        foreach ($batch_list as $v){
            $new_batch_arr[date('Y-m',$v['create_time'])][] = $v;

        }
        //获取订单总价
        $purchase_arr = $this->get_oder_price();
        $batch_arr = array();
        $batch_all_arr = array();
        foreach ($new_batch_arr as $k=>$v){
            $batch_arr['time'] = $k;
            $batch_arr['state'] = 1;
            $batch_arr['items'] = array();
            $batch_arr['payment_way'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式
            foreach ($v as $vv){
                $arr['id'] = (string)$vv['_id'];
                $arr['money'] = isset($purchase_arr[$vv['_id'].''])?$purchase_arr[$vv['_id'].'']:0;
                $arr['time'] = date('Y-m-d',$vv['create_time']);
                array_push($batch_arr['items'],$arr);
            }
            array_push($batch_all_arr,$batch_arr);
        }
        //待付款账单
        $batch_ls = $batch_model->find(array('status'=>20,'supplier_id'=>(string)$supplier_info['_id']))->sort(array('create_time'=>-1));
        $new_batch_arr1 = array();
        foreach ($batch_ls as $v){
            $new_batch_arr1[date('Y-m',$v['create_time'])][] = $v;
        }
        $batch_arr1 = array();
        $batch_all_arr1 = array();
        foreach ($new_batch_arr1 as $k=>$v){
            $batch_arr1['time'] = $k;
            $batch_arr1['state'] = 2;
            $batch_arr1['items'] = array();
            $batch_arr1['payment_way'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式
            foreach ($v as $vv){
                $arr1['id'] = (string)$vv['_id'];
                $arr1['money'] = isset($purchase_arr[$vv['_id'].''])?$purchase_arr[$vv['_id'].'']:0;
                $arr1['time'] = date('Y-m-d',$vv['create_time']);
                array_push($batch_arr1['items'],$arr1);
            }
            array_push($batch_all_arr,$batch_arr1);
        }

        //已付款账单
        $batch_ls1 = $batch_model->find(array('status'=>30,'supplier_id'=>(string)$supplier_info['_id']))->sort(array('create_time'=>-1));
        $new_batch_arr2 = array();
        foreach ($batch_ls1 as $v){
            $new_batch_arr2[date('Y-m',$v['create_time'])][] = $v;
        }
        $batch_arr2 = array();
        $batch_all_arr2 = array();
        foreach ($new_batch_arr2 as $k=>$v){
            $batch_arr2['time'] = $k;
            $batch_arr2['state'] = 3;
            $batch_arr2['items'] = array();
            $batch_arr2['payment_way'] = @empty($payment_way_arr[$supplier_info['checkout_method']])?'无':$payment_way_arr[$supplier_info['checkout_method']];//结算方式
            foreach ($v as $vv){
                $arr2['id'] = (string)$vv['_id'];
                $arr2['money'] = isset($purchase_arr[$vv['_id'].''])?$purchase_arr[$vv['_id'].'']:0;
                $arr2['time'] = date('Y-m-d',$vv['create_time']);
                array_push($batch_arr2['items'],$arr2);
            }
            array_push($batch_all_arr,$batch_arr2);
        }
//        $data = array();
//        $data[] = $batch_all_arr;
//        $data[] = $batch_all_arr1;
//        $data[] = $batch_all_arr2;

        $this->success($batch_all_arr);
    }
    public function get_oder_price(){
        //计算订单的总价
        $purchase_model = new purchase_order_new();
        $purchase_list = $purchase_model->aggregate(
            array(
                array(
                    '$group'=>array(
                        '_id'=>array(
                            'batch_id'=>'$batch_id'
                        ),
                        'total_price'=>array('$sum'=>'$product_info.total_price'),
                        'batch_id'=>array('$first'=>'$batch_id')
                    ),
                ),
                array(
                    '$sort'=>array(
                        'create_time'=>-1
                    )
                )
            )
        );
        $purchase_arr = array();
        foreach ($purchase_list as $v){
            if(is_array($v)){
                foreach ($v as $vv){
                    if(!isset($vv['batch_id'])){
                        continue;
                    }
                    $purchase_arr[$vv['batch_id'].''] = isset($vv['total_price'])?round($vv['total_price']/100,2):0;
                }
            }
        }
        return $purchase_arr;
    }

    public function Marvin_test() {
        $ip = $_GET['ip'];
        $ip = gethostbyname("$ip");
        //var_dump($ip);
        if(empty($ip)){
            $ip = $_SERVER["REMOTE_ADDR"];
        }
        $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
        if(empty($res)){ return false; }
        $jsonMatches = array();
        preg_match('#\{.+?\}#', $res, $jsonMatches);
        if(!isset($jsonMatches[0])){ return false; }
        $json = json_decode($jsonMatches[0], true);
        if(isset($json['ret']) && $json['ret'] == 1){
            $json['ip'] = $ip;
            unset($json['ret']);
        }else{
            return false;
        }

        echo '<br/>国家---'.$json['country'].'<br/>';
        echo '省/州---'.$json['province'].'<br/>';
        echo '市---'.$json['city'].'<br/>';
        echo '您的Ip---'.$json['ip'].'<br/>';

    }
}

